﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Entity.IRepository.Project;
using Entity.ProjectEnt;
using Logic.Repository.Project;
using ZFine.Code;

namespace ZFine.Application.Project
{
    public class CustomerAndThingsNameApp
    {
        private ICustomerAndThingsNameRepository service = new CustomerAndThingsNameRepository();
        public List<CustomerAndThingsNameEntity> GetList(string key,bool? allowFagnxing=null)
        {
            var expression = ExtLinq.True<CustomerAndThingsNameEntity>();
            expression = expression.And(t => t.State == true);
            if (key == "0")
            {
                expression = expression.And(t => t.Type == 0);
            }
            else if (key == "1")
            {
                expression = expression.And(t => t.Type == 1);
            }
            else if (key == "2") {
                expression = expression.And(t => t.Type == 2);
            }
            if (allowFagnxing != null && allowFagnxing == false)
            {
                expression = expression.And(t => t.AllowFangxing == allowFagnxing);
            }
            else if (allowFagnxing != null && allowFagnxing == true) {
                expression = expression.And(t => t.AllowFangxing == allowFagnxing||t.AllowFangxing==null);
            }
            return service.IQueryable(expression).ToList();
        }
        //public List<CustomerAndThingsNameEntity> GetListCustomer(string key)
        //{
        //    var expression = ExtLinq.True<CustomerAndThingsNameEntity>();
        //    expression = expression.And(t => t.State == true);
        //    expression = expression.And(t => t.Type == 1);
        //    return service.IQueryable(expression).ToList();
        //}
        //public List<CustomerAndThingsNameEntity> GetListThingsName()
        //{
        //    var expression = ExtLinq.True<CustomerAndThingsNameEntity>();
        //    expression = expression.And(t => t.State == true);
        //    expression = expression.And(t => t.Type == 0);
        //    return service.IQueryable(expression).ToList();
        //}
        public List<CustomerAndThingsNameEntity> GetList(Expression<Func<CustomerAndThingsNameEntity, bool>> predicate)
        {
            return service.IQueryable(predicate).ToList();
        }
        public List<CustomerAndThingsNameEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<CustomerAndThingsNameEntity>();
            expression = expression.And(t => t.State == true);
            return service.FindList(expression, pagination);
        }
        public List<CustomerAndThingsNameEntity> GetListD(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<CustomerAndThingsNameEntity>();
            expression = expression.And(t => t.State == true);
            return service.FindList(expression, pagination);
        }
        public CustomerAndThingsNameEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public int DeleteForm(string keyValue)
        {
            var local = service.FindEntity(keyValue);
            if (local != null)
            {
                local.State = false;
                return service.Update(local);
            }
            else
            {
                return -1;
            }
        }
        public int SubmitForm(CustomerAndThingsNameEntity ServiceEntity, string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (LoginInfo != null)
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    ServiceEntity.State = true;
                    ServiceEntity.Type = 0;
                    ServiceEntity.Modify(keyValue);
                    return service.Update(ServiceEntity);
                }
                else
                {
                    ServiceEntity.Create();
                    ServiceEntity.State = true;
                    ServiceEntity.Type = 0;
                    return service.Insert(ServiceEntity);
                }
            }
            return -1;
        }


        public int SubmitFormCarBoxType(CustomerAndThingsNameEntity ServiceEntity, string keyValue) {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (LoginInfo != null)
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    ServiceEntity.State = true;
                    ServiceEntity.Type = 2;
                    ServiceEntity.Modify(keyValue);
                    return service.Update(ServiceEntity);
                }
                else
                {
                    ServiceEntity.Create();
                    ServiceEntity.State = true;
                    ServiceEntity.Type = 2;
                    return service.Insert(ServiceEntity);
                }
            }
            return -1;
        }
        public int SubmitFormCustomer(CustomerAndThingsNameEntity ServiceEntity, string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (LoginInfo != null)
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    ServiceEntity.State = true;
                    ServiceEntity.Type = 1;
                    ServiceEntity.Modify(keyValue);
                    return service.Update(ServiceEntity);
                }
                else
                {
                    ServiceEntity.Create();
                    ServiceEntity.State = true;
                    ServiceEntity.Type = 1;
                    return service.Insert(ServiceEntity);
                }
            }
            return -1;
        }
    }
}
